package com.example.zhihu.not;

import com.example.zhihu.base.BaseApp;

import java.io.File;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class HttpUlist {

    private final Retrofit.Builder retrofit;

    public HttpUlist(){
        OkHttpClient okHttpClient = getOkHttpClient();
        retrofit = getRetrofit(okHttpClient);
    }

    private Retrofit.Builder getRetrofit(OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create());
    }
    public static volatile HttpUlist instance;
    public static HttpUlist getInstance(){
        if (instance==null){
            synchronized (HttpUlist.class){
                if (instance==null){
                    instance=new HttpUlist();
                }
            }
        }
        return instance;
    }
    private OkHttpClient getOkHttpClient() {
       // String PATH_DATA=BaseApp.baseApp.getCacheDir().getAbsolutePath()+File.separator+"data";
       // String PATH_CACHE=PATH_DATA+"/NetCahe";
       // File file = new File(PATH_CACHE);
       // Cache cache = new Cache(file,1024 * 1024 * 100);
        return new OkHttpClient.Builder()
               // .cache(cache)
                .readTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60,TimeUnit.SECONDS)
                .writeTimeout(60,TimeUnit.SECONDS)
                .retryOnConnectionFailure(true)
                .build();
    }
    public synchronized <T> T getApiServer(String url,Class<T> tClass){
        return retrofit.baseUrl(url).build().create(tClass);
    }
}
